<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提货券修改</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${jspath}/jquery/jquery-2.1.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>

<script type="text/javascript" src="${layerPath}/layer.js"></script>
</head>


<script type="text/javascript">
	$(function() {

		$("#couponForm").validation({
			icon : true
		});
		//返回
		$("#returnBtn")
				.click(
						function() {
							$("#couponForm")
									.attr('action',
											'${webcontext}/crmdp/dealer/coupon/toDefineTicKet');
							$("#couponForm").submit();
						});

		//修改保存
		$("#saveBtn")
				.click(
						function() {
							if ($("#couponForm").valid(this) == false) {
								return false;
							}
							var radio = document.getElementsByName("ifPrepay");
							var ifPrepay = null;
							for ( var i = 0; i < radio.length; i++) {
								if (radio[i].checked == true) {
									ifPrepay = radio[i].value;
									break;
								}
							}
							$
									.ajax({
										url : '${webcontext}/crmdp/dealer/coupon/updateTicketInfo',
										type : 'post',
										data : {
											'id' : $("#ticketDefId").val(),
											'ticketName' : $("#ticketName")
													.val(),
											'ticketType' : $("#ticketType")
													.val(),
											'ifPrepay' : ifPrepay,
											'verificationMethod' : $(
													"#verificationMethod")
													.val(),
											'deductionMethod' : $(
													"#deductionMethod").val(),
											'deductionAmount' : $(
													"#deductionAmount").val()
										},
										success : function(data) {
											if (data.code == true) {
												jAlert(
														data.msg,
														"提示",
														function() {
															$("#couponForm").attr('action','${webcontext}/crmdp/dealer/coupon/toEditTicket');
															$("#couponForm").submit();
														});
											} else if (data.code == false) {
												jAlert(data.msg, "提示",function(){
													$("#couponForm").attr('action','${webcontext}/crmdp/dealer/coupon/toEditTicket');
													$("#couponForm").submit();
												});
											}
										},
										error : function() {
											jAlert("服务器错误，请联系管理员", "提示");
										},
									});
						});

	});

	window.onload = function() {
		var ifPrepay = '${ticket.ifPrepay}';
		if (ifPrepay != null & ifPrepay != "") {
			if (ifPrepay == 1) {
				$("input[name='ifPrepay'][value=1]").attr("checked", true);
			} else if (ifPrepay == 0) {
				$("input[name='ifPrepay'][value=0]").attr("checked", true);
			}
		}
		var verificationMethod = '${ticket.verificationMethod}';
		if (verificationMethod != null & verificationMethod != "") {
			$("#verificationMethod").val(verificationMethod);
		}

		var deductionMethod = '${ticket.deductionMethod}';
		if (deductionMethod != null & deductionMethod != "") {
			$("#deductionMethod").val(deductionMethod);
		}
	};

	//删除优惠券的详细信息
	function removeRedemptionLine(id) {
		$.ajax({
			url : '${webcontext}/crmdp/dealer/coupon/checkPackageByTicketDefId',
			type : 'post',
			data : {
				'ticketDefId' : $("#ticketDefId").val()
			},
			success : function(data) {
				if (data.code == true) {
					//询问框
					jConfirm(
							"确定要删除数据吗？",
							"确认",
							function(r) {
								if (r) {
									$
											.ajax({
												url : '${webcontext}/crmdp/dealer/coupon/removeTiketLineInfo',
												type : 'post',
												data : {
													'id' : id,
													'ticketDefId' : $("#ticketDefId").val()
												},
												success : function(data) {
													if (data.code == true) {
														jAlert(
																data.msg,
																"提示",
																function() {
																	$("#couponForm").attr('action','${webcontext}/crmdp/dealer/coupon/toEditTicket');
																	$("#couponForm").submit();
																});
													} else if (data.code == false) {
														jAlert(data.msg, "提示");
													}
												},
												error : function() {
													jAlert("服务器错误，请联系管理员", "提示");
												},
											});
								}
							});
				} else if (data.code == false) {
					jAlert(data.msg, "提示");
				}
			},
			error : function() {
				jAlert("服务器错误，请联系管理员", "提示");
			},
		});
	}

	//修改优惠券的详细信息
	function editRedemptionLineInfo(id) {
		$.ajax({
			url : '${webcontext}/crmdp/dealer/coupon/checkPackageByTicketDefId',
			type : 'post',
			data : {
				'ticketDefId' : $("#ticketDefId").val()
			},
			success : function(data) {
				if (data.code == true) {
					var qticketType = $("#qticketType").val();
					var qticketName = $("#qticketName").val();
					var pager = $("#pager").val();
					layer
							.open({
								type : 2,
								title : '产品修改',
								shadeClose : false,
								closeBtn : false,
								shade : 0.1,
								offset : [ '16%', '28%' ],
								area : [ '540px', '300px' ],
								content : '${webcontext}/crmdp/dealer/coupon/toRedemptionLinePage?id='
										+ id
										+ "&qticketType="
										+ qticketType
										+ "&qticketName="
										+ qticketName
										+ "&pager.currentPage=" + pager
							});
				} else if (data.code == false) {
					jAlert(data.msg, "提示");
				}
			},
			error : function() {
				jAlert("服务器错误，请联系管理员", "提示");
			},
		});
	}

	//新增产品
	function insertRow() {
		$.ajax({
			url : '${webcontext}/crmdp/dealer/coupon/checkPackageByTicketDefId',
			type : 'post',
			data : {
				'ticketDefId' : $("#ticketDefId").val()
			},
			success : function(data) {
				if (data.code == true) {
					var ticketDefId = $("#ticketDefId").val();
					var qticketType = $("#qticketType").val();
					var qticketName = $("#qticketName").val();
					var pager = $("#pager").val();
					layer
							.open({
								type : 2,
								title : '项目新增',
								shadeClose : false,
								closeBtn : false,
								shade : 0.1,
								offset : [ '16%', '28%' ],
								area : [ '500px', '300px' ],
								content : '${webcontext}/crmdp/dealer/coupon/saveRedemptionLineDetails?ticketDefId='
										+ ticketDefId
										+ "&qticketType="
										+ qticketType
										+ "&qticketName="
										+ qticketName
										+ "&pager.currentPage=" + pager
							});
				} else if (data.code == false) {
					jAlert(data.msg, "提示");
				}
			},
			error : function() {
				jAlert("服务器错误，请联系管理员", "提示");
			},
		});
	}

	
	//适用范围
	function toScopeOfTicket(o){
		$.ajax({
			url : '${webcontext}/crmdp/dealer/coupon/checkPackageByTicketDefId',
			type : 'post',
			data : {
				'ticketDefId' : $("#ticketDefId").val()
			},
			success : function(data) {
				if (data.code == true) {
					var  url = "${webcontext}/crmdp/dealer/coupon/toScopeOfTicket?ticketLineId=";
					layer.open({
					    type: 2,
					    title: '适用范围',
					    shadeClose: false,
					    closeBtn: false,
					    shade: 0.1, 
					    offset : ['12%' , '15%'],
					    area: ['800px', '500px'],
					    content: url + o,//iframe的url
//			 		    end: function(){ 
//			 		    	//关闭弹出层后回调方法
//			 		    	window.location.href="${webcontext}/crmdp/dealer/member/redirectMaintainMemberPage";
//			 		    }
					}); 
				} else if (data.code == false) {
					jAlert(data.msg, "提示");
				}
			},
			error : function() {
				jAlert("服务器错误，请联系管理员", "提示");
			},
		});
	}


	function priceManage(id) {
		$.ajax({
			url : '${webcontext}/crmdp/dealer/coupon/checkPackageByTicketDefId',
			type : 'post',
			data : {
				'ticketDefId' : $("#ticketDefId").val()
			},
			success : function(data) {
				if (data.code == true) {
					$("#ticketLineId").val(id);
					$("#couponForm").attr('action',
							'${webcontext}/crmdp/dealer/coupon/toTicketLinePricePage');
					$("#couponForm").submit();
				} else if (data.code == false) {
					jAlert(data.msg, "提示");
				}
			},
			error : function() {
				jAlert("服务器错误，请联系管理员", "提示");
			},
		});
	}

</script>
<style>
.hr {
	text-align: center;
	width: 100%;
	background-color: #0071C1;
	height: 1px;
	margin-bottom: 10px;
}

.rowDiv {
	width: 90%;
}

.formDiv {
	float: left;
	width: 50%;
}

.textarea {
	width: 700px;
	height: 100px;
	max-width: 700px;
	max-height: 100px;
	resize: none;
}

.btnLength {
	width: 70px;
}
</style>
<body>
	<div id="content" style="height: 950px;">
		<div style="color: #025D7B; height: 26px; line-height: 26px;">
			<span>提货券定义</span> <span>&gt;</span> <span>提货券修改</span>
		</div>
		<div class="hr"></div>
		<form:form id="couponForm" action="" class="form-horizontal"
			method="post" modelAttribute="dto" role="form">
			<input type="text" class="hidden" id="ticketDefId" name="ticketDefId"
				value="${ticket.id}" />
			<input type="text" class="hidden" id="ticketLineId"
				name="ticketLineId" />
			<input type="text" class="hidden" id="ticketType" name="ticketType"
				value="${ticket.ticketType}" />

			<input type="text" class="hidden" id="qticketType" name="qticketType"
				value="${qticketType}" />
			<input type="text" class="hidden" id="qticketName" name="qticketName"
				value="${qticketName}" />

			<input type="text" class="hidden" id="pager" name="pager.currentPage"
				value="${currentPage}">
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="ticketName">名称</label>
					<div class="col-sm-6">
						<input class="form-control input-sm" id="ticketName"
							name="ticketName" type="text" value="${ticket.ticketName}"
							check-type="required" required-message="请填写名称" disabled />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="deductionMethod">抵扣方式</label>
					<div class="col-sm-6">
						<select id="deductionMethod" class="form-control input-sm">
							<option value="AMOUNT">金额</option>
							<option value="PERCENT">百分比</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="deductionAmount">抵扣值</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right"
							id="deductionAmount" type="text"
							value="${ticket.deductionAmount}" check-type="number" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="verificationMethod">抵扣凭证</label>
					<div class="col-sm-6">
						<select id="verificationMethod" class="form-control input-sm">
							<option value="CODE">抵扣码</option>
							<option value="N/A">无</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="prepayAmount">金额</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right" id="prepayAmount"
							value="${prepayAmount}" type="text" readonly />
					</div>
				</div>
			</div>
			<div class="row rowDiv hidden">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="ifPrepay">是否预付费</label>
					<label class="radio-inline"> <input type="radio"
						name="ifPrepay" id="ifPrepay1" value="1">是
					</label> <label class="radio-inline"> <input type="radio"
						name="ifPrepay" id="ifPrepay2" value="0">否
					</label>
				</div>
			</div>
		</form:form>
		<div style="margin-left: 300px;">
			<button type="button" class="btn btn-success btn-sm" id="saveBtn">
				<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保
				存
			</button>
			<button type="button" class="btn btn-success btn-sm"
				onclick="insertRow()">
				<i class="glyphicon glyphicon-plus" style="color: #FFFFFF"></i>&nbsp;添加产品
			</button>
			<button type="button" class="btn btn-success btn-sm" id="returnBtn">
				<i class="glyphicon glyphicon-retweet" style="color: #FFFFFF"></i>&nbsp;返回
			</button>
		</div>
		<form:form id="search_form" action="" class="form-horizontal"
			method="post" modelAttribute="dto" role="form">
			<table id="data_tabs" class="table"
				style="margin-top: 20px; margin-left: 20px; width: 70%;">
				<thead>
					<tr>
						<th width="5%" class="hidden">ID</th>
						<th width="20%">产品说明</th>
						<th width="6%">数量</th>
						<th width="10%">金额</th>
						<th width="30%">操作</th>
					</tr>
				</thead>
				<tbody>
					<core:forEach items="${ticketLineList}" var="list">
						<tr>
							<td class="hidden">${list.id}</td>
							<td>${list.itemName}</td>
							<td class="text-right">${list.quantity}</td>
							<td class="text-right">${list.prepayAmount}</td>
							<td class="text-left">
								<button type="button" class="btn btn-success btn-xs btnLength"
									onClick="editRedemptionLineInfo('${list.id}')">修改</button>
								<button type="button" class="btn btn-success btn-xs btnLength"
									onClick="toScopeOfTicket('${list.id}')">适用范围</button>
								<button type="button" class="btn btn-success btn-xs width:300px"
									onclick="priceManage('${list.id}')">价目表管理</button>
								<button type="button" class="btn btn-success btn-xs btnLength"
									onClick="removeRedemptionLine('${list.id}')">删除</button>
							</td>
						</tr>
					</core:forEach>
				</tbody>
			</table>
		</form:form>
	</div>
</body>
</html>